Code
library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.1 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Code
library(dataRetrieval)
library(ggplot2)
library(plotly)
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
Code
library(lubridate)
library(ggpubr)
library(plotly)
library(dplyr)
library(tsibble)Registered S3 method overwritten by 'tsibble':
method from
as_tibble.grouped_df dplyr
Attaching package: 'tsibble'
The following object is masked from 'package:lubridate':
interval
The following objects are masked from 'package:base':
intersect, setdiff, union
Code
# CO unprotected 1
QCO_unpro1=readNWISdv(
siteNumber= c('09034250'),
parameterCd= c('00060'),
startDate='1975-10-01', #beginning of wateryear 1976
endDate='2024-10-01') %>%
renameNWISColumns() %>%
mutate(Date = yearmonth(Date)) %>%
group_by(site_no, Date) %>%
summarise(Flow = mean(Flow, na.rm = TRUE), .groups = "drop")
pHWT_COunpro1 <- read.csv(
"https://raw.githubusercontent.com/snauman4/ESS330_FinalProject/main/CO_dataunprotected1.csv",
stringsAsFactors = FALSE
)
# 2. Compute daily means of Temp and pH
DailypW_COunpro1 <- pHWT_COunpro1 %>%
# parse your Date
mutate(Date = mdy(Date)) %>%
# drop the qualifier columns and coerce the true max/min to numeric
mutate(across(
c(Temp..max., Temp..min., pH..max., pH..min.),
~ as.numeric(.)
)) %>%
# compute a rowwise mean of temp and of pH
mutate(
Temp_mean = rowMeans(select(., Temp..max., Temp..min.), na.rm = TRUE),
pH_mean = rowMeans(select(., pH..max., pH..min.), na.rm = TRUE)
) %>%
group_by(site_no, Date) %>%
summarise(
`Temp (C)` = mean(Temp_mean, na.rm = TRUE),
`pH (mean)` = mean(pH_mean, na.rm = TRUE),
.groups = "drop"
)Warning: There was 1 warning in `mutate()`.
ℹ In argument: `across(c(Temp..max., Temp..min., pH..max., pH..min.),
~as.numeric(.))`.
Caused by warning:
! NAs introduced by coercion
Code
# 3. Roll those daily means up to monthly to line up with your flow data
Month_COunpro1 <- DailypW_COunpro1 %>%
mutate(
Date = yearmonth(Date)
) %>%
group_by(site_no, Date) %>%
summarise(
`Temp (C)` = mean(`Temp (C)`, na.rm = TRUE),
`pH (mean)` = mean(`pH (mean)`, na.rm = TRUE),
.groups = "drop"
)
# make sure site_no is character in both data sets
QCO_unpro1 <- QCO_unpro1 %>%
mutate(
site_no = as.character(site_no), # ensure it’s a string
Date = yearmonth(Date) # ensure it’s a yearmonth
)
Month_COunpro1 <- Month_COunpro1 %>%
mutate(
site_no = str_pad(as.character(site_no), width = 8, pad = "0"),
# pad out to 8 digits with leading zeros
Date = yearmonth(Date)
)
# now the join will work
COdata_unprotected1 <- QCO_unpro1 %>%
left_join(Month_COunpro1, by = c("site_no","Date")) %>%
select(site_no, Date, Flow, `Temp (C)`, `pH (mean)`)
# CO unprotected 2
QCO_unpro2=readNWISdv(
siteNumber= c('09147500'),
parameterCd= c('00060'),
startDate='1975-10-01', #beginning of wateryear 1976
endDate='2024-10-01') %>%
renameNWISColumns() %>%
mutate(Date = yearmonth(Date)) %>%
group_by(site_no, Date) %>%
summarise(Flow = mean(Flow, na.rm = TRUE), .groups = "drop")
pHWT_COunpro2 <- read.csv(
"https://raw.githubusercontent.com/snauman4/ESS330_FinalProject/main/CO_dataunprotected2.csv",
stringsAsFactors = FALSE
)
# 2. Compute daily means of Temp and pH
DailypW_COunpro2 <- pHWT_COunpro2 %>%
# parse your Date
mutate(Date = mdy(Date)) %>%
# drop the qualifier columns and coerce the true max/min to numeric
mutate(across(
c(Temp..max., Temp..min.),
~ as.numeric(.)
)) %>%
# compute a rowwise mean of temp and of pH
mutate(
Temp_mean = rowMeans(select(., Temp..max., Temp..min.), na.rm = TRUE)
) %>%
group_by(site_no, Date) %>%
summarise(
`Temp (C)` = mean(Temp_mean, na.rm = TRUE),
.groups = "drop"
)Warning: There was 1 warning in `mutate()`.
ℹ In argument: `across(c(Temp..max., Temp..min.), ~as.numeric(.))`.
Caused by warning:
! NAs introduced by coercion
Code
# 3. Roll those daily means up to monthly to line up with your flow data
Month_COunpro2 <- DailypW_COunpro2 %>%
mutate(
Date = yearmonth(Date)
) %>%
group_by(site_no, Date) %>%
summarise(
`Temp (C)` = mean(`Temp (C)`, na.rm = TRUE),
.groups = "drop"
)
# make sure site_no is character in both data sets
QCO_unpro2 <- QCO_unpro2 %>%
mutate(
site_no = as.character(site_no), # ensure it’s a string
Date = yearmonth(Date) # ensure it’s a yearmonth
)
Month_COunpro2 <- Month_COunpro2 %>%
mutate(
site_no = str_pad(as.character(site_no), width = 8, pad = "0"),
# pad out to 8 digits with leading zeros
Date = yearmonth(Date)
)
# now the join will work
COdata_unprotected2 <- QCO_unpro2 %>%
left_join(Month_COunpro2, by = c("site_no","Date")) %>%
select(site_no, Date, Flow, `Temp (C)`)
# CA unprotected
QCA_unpro=readNWISdv(
siteNumber= c('11074000'),
parameterCd= c('00060'),
startDate='1975-10-01', #beginning of wateryear 1976
endDate='2024-10-01') %>%
renameNWISColumns() %>%
mutate(Date = yearmonth(Date)) %>%
group_by(site_no, Date) %>%
summarise(Flow = mean(Flow, na.rm = TRUE), .groups = "drop")
pHWT_CAunpro <- read.csv(
"https://raw.githubusercontent.com/snauman4/ESS330_FinalProject/main/CA_dataunprotected.csv",
stringsAsFactors = FALSE
)
# 2. Compute daily means of Temp and pH
DailypW_CAunpro <- pHWT_CAunpro %>%
# parse your Date
mutate(Date = mdy(Date)) %>%
# drop the qualifier columns and coerce the true max/min to numeric
mutate(across(
c(Temp..max., Temp..min.),
~ as.numeric(.)
)) %>%
# compute a rowwise mean of temp and of pH
mutate(
Temp_mean = rowMeans(select(., Temp..max., Temp..min.), na.rm = TRUE)
) %>%
group_by(site_no, Date) %>%
summarise(
`Temp (C)` = mean(Temp_mean, na.rm = TRUE),
.groups = "drop"
)
# 3. Roll those daily means up to monthly to line up with your flow data
Month_CAunpro <- DailypW_CAunpro %>%
mutate(
Date = yearmonth(Date)
) %>%
group_by(site_no, Date) %>%
summarise(
`Temp (C)` = mean(`Temp (C)`, na.rm = TRUE),
.groups = "drop"
)
# make sure site_no is character in both data sets
QCA_unpro <- QCA_unpro %>%
mutate(
site_no = as.character(site_no), # ensure it’s a string
Date = yearmonth(Date) # ensure it’s a yearmonth
)
Month_CAunpro <- Month_CAunpro %>%
mutate(
site_no = str_pad(as.character(site_no), width = 8, pad = "0"),
Date = yearmonth(Date))
CAdata_unprotected <- QCA_unpro %>%
left_join(Month_CAunpro, by = c("site_no","Date")) %>%
select(site_no, Date, Flow, `Temp (C)`)
# AZ unprotected
AZunproraw <- read.csv(
"https://raw.githubusercontent.com/madi-schartz/ESS330-FINAL/refs/heads/master/AZdata_unprotected.csv",
stringsAsFactors = FALSE
)
# add pH
DailypW_AZunpro <- AZunproraw %>%
mutate(Date = mdy(Date)) %>%
mutate(across(
c(Temp..mean.),
~ as.numeric(.)
)) %>%
group_by(site_no, Date) %>%
summarise(
Flow = mean(Discharge..Mean., na.rm = TRUE),
`Temp (C)` = mean(Temp..mean., na.rm = TRUE),
.groups = "drop"
)
AZdata_unprotected <- DailypW_AZunpro %>%
mutate(Date = yearmonth(Date)) %>%
group_by(site_no, Date) %>%
summarise(
Flow = mean(Flow, na.rm = TRUE),
`Temp (C)` = mean(`Temp (C)`, na.rm = TRUE),
.groups = "drop"
)
# UT unprotected
UTunproraw <- read.csv(
"https://raw.githubusercontent.com/madi-schartz/ESS330-FINAL/refs/heads/master/UTdata_unprotected.csv",
stringsAsFactors = FALSE
)
DailypW_UTunpro <- UTunproraw %>%
mutate(Date = mdy(Date)) %>%
mutate(across(
c(Discharge,`Temp..Min.`,`Temp..Max.`, `pH..Max.`, `pH..Min.`),
~ as.numeric(.)
)) %>%
mutate(
Temp_mean = rowMeans(select(., Temp..Max., Temp..Min.), na.rm = TRUE),
pH_mean = rowMeans(select(., pH..Max., pH..Min.), na.rm = TRUE)
) %>%
group_by(site_no, Date) %>%
summarise(
Flow = mean(Discharge, na.rm = TRUE),
`Temp (C)` = mean(Temp_mean, na.rm = TRUE),
`pH (mean)`= mean(pH_mean, na.rm = TRUE),
.groups = "drop"
)Warning: There was 1 warning in `mutate()`.
ℹ In argument: `across(...)`.
Caused by warning:
! NAs introduced by coercion
Code
# 3. roll up to monthly
UTdata_unprotected <- DailypW_UTunpro %>%
mutate(Date = yearmonth(Date)) %>%
group_by(site_no, Date) %>%
summarise(
Flow = mean(Flow, na.rm = TRUE),
`Temp (C)` = mean(`Temp (C)`, na.rm = TRUE),
`pH (mean)`= mean(`pH (mean)`,na.rm = TRUE),
.groups = "drop"
)